﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace GreedRoller
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.listBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.listBox1_KeyPress);    //注册事件
            this.listBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.listBox2_KeyPress);
        }

        protected string[] wod_roll(int dp, int again)     //掷骰处理
        {
            System.Threading.Thread.Sleep(9);
            System.Random roll = new Random(System.DateTime.Now.Millisecond);

            string[] result = new string[3] { "", "", "" };

            int dc = 8;

            int success = 0;
            int addition = 0;
            string display = "";

            int rnd = 0;

            for (int i = 0; i < dp; i++)
            {
                rnd = roll.Next(1, 11);

                if (rnd >= dc)
                {
                    success++;

                    if (rnd >= again)
                    {
                        addition++;
                    }

                    display += rnd.ToString();
                }
                else
                {
                    display += rnd.ToString();
                }

                display += " ";
            }

            result[0] = success.ToString();
            result[1] = addition.ToString();
            result[2] = display;

            return result;
        }

        protected string[] loop(int dp, int again, int autosuccess)      //循环处理加骰
        {
            string[] final = new string[3] { "", "", "" };

            int f_success = 0;
            string f_display = "";
            string add_display="";   //加骰细节

            final = wod_roll(dp, again);
            f_success += Convert.ToInt32(final[0]);
            f_display += final[2];

            while (Convert.ToInt32(final[1]) != 0)
            {
                final = wod_roll(Convert.ToInt32(final[1]), again);
                f_success += Convert.ToInt32(final[0]);
                if (final[2] != "")
                {
                    add_display += " + (" + final[2] + ")";              //加骰细节
                }
            }

            if (f_success != 0)
            {
                f_success = f_success + autosuccess;
            }
            string[] f_result = new string[2] { "", "" };

            f_result[0] = f_success.ToString() + " ( " + f_display + ")" ;
            if(add_display != "") 
            {
                f_result[0] += add_display;
            }
            f_result[1] = f_success.ToString();

            return f_result;
        }

        protected void selected_vs()    //选中项求差
        {
            int num1 = 0;
            int num2 = 0;

            try
            {
                num1 = Convert.ToInt32(((ListItem)this.listBox1.SelectedItem).ID);
            }
            catch
            {
                num1 = 0;
            }

            try
            {
                num2 = Convert.ToInt32(((ListItem)this.listBox2.SelectedItem).ID);
            }
            catch
            {
                num2 = 0;
            }

            richTextBox1.Text = Convert.ToString(num1 - num2);

            newest_vs();
        }

        protected void newest_vs()      //最新项求差
        {
            int newest_roll_1 = 0;
            int newest_roll_2 = 0;

            if (this.listBox1.Items.Count == 0)
            {
                newest_roll_1 = 0;
            }
            else
            {
                newest_roll_1 = Convert.ToInt32(((ListItem)this.listBox1.Items[this.listBox1.Items.Count - 1]).ID);
            }

            if (this.listBox2.Items.Count == 0)
            {
                newest_roll_2 = 0;
            }
            else
            {
                newest_roll_2 = Convert.ToInt32(((ListItem)this.listBox2.Items[this.listBox2.Items.Count - 1]).ID);
            }

            richTextBox2.Text = Convert.ToString(newest_roll_1 - newest_roll_2);
        }

        private void button1_Click(object sender, EventArgs e)      //Roll 1
        {
            string[] item_source = loop(Convert.ToInt32(this.numericUpDown1.Value), Convert.ToInt32(this.numericUpDown2.Value), Convert.ToInt32(this.numericUpDown5.Value));

            string item_text = item_source[1];
            string item_value = item_source[1];
            if (checkBox1.Checked)     //极其丑陋的解决办法 byBX
            {
                item_text = item_source[0];
            }

            ListItem item = new ListItem(item_value, item_text);
            this.listBox1.Items.Add(item);
            this.listBox1.ValueMember = "ID"; 
            this.listBox1.DisplayMember = "Name";

            listBox1.SelectedItems.Clear();
            listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];

            newest_vs();
        }

        private void button2_Click(object sender, EventArgs e)      //Clear 1
        {
            this.listBox1.Items.Clear();
        }

        private void button3_Click(object sender, EventArgs e)      //Roll 2
        {
            string[] item_source = loop(Convert.ToInt32(this.numericUpDown3.Value), Convert.ToInt32(this.numericUpDown4.Value), Convert.ToInt32(this.numericUpDown6.Value));

            string item_text = item_source[1];
            string item_value = item_source[1];
            if (checkBox1.Checked)     //极其丑陋的解决办法 byBX
            {
                item_text = item_source[0];
            }

            ListItem item = new ListItem(item_value, item_text);
            this.listBox2.Items.Add(item);
            this.listBox2.ValueMember = "ID";
            this.listBox2.DisplayMember = "Name";

            listBox2.SelectedItems.Clear();
            listBox2.SelectedItem = listBox2.Items[listBox2.Items.Count - 1];

            newest_vs();
        }

        private void button4_Click(object sender, EventArgs e)      //Clear 2
        {
            this.listBox2.Items.Clear();
        }


        private void listBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)     //List 1 Copy
        {
            if (e.KeyChar == 3)   //Crtl+C  
            {
                string val = "";
                for (int i = 0; i < listBox1.SelectedItems.Count; i++)
                {
                    val += listBox1.SelectedItems[i].ToString() + "\r\n";
                }
                Clipboard.SetDataObject(val);   
            }
        }

        private void listBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)     //List 2 Copy
        {
            if (e.KeyChar == 3)   //Crtl+C  
            {
                string val = "";
                for (int i = 0; i < listBox2.SelectedItems.Count; i++)
                {
                    val += listBox2.SelectedItems[i].ToString() + "\r\n";
                }
                Clipboard.SetDataObject(val);
            }
        }


        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            selected_vs();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            selected_vs();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void numericUpDown6_ValueChanged(object sender, EventArgs e)
        {
        
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form2 Initia=new Form2(this);
            Initia.ShowDialog();
        }

        //这函数现在没啥用了
        private void diceinput_TextChanged(object sender, EventArgs e)
        {
            //string pattern = "\\S";
            //diceoutput.Text=Regex.Replace(diceinput.Text,pattern,"");
            //diceoutput.ge
            //diceoutput.ScrollToCaret();
        }

        //单击开始投骰事件
        private void diceinput_MouseClick(object sender, MouseEventArgs e)
        {
            if (start_rolling.Checked == false) return;
            int curent_char = diceinput.GetCharIndexFromPosition(e.Location);
            //diceoutput.Text = curent_char.ToString();
            int curent_line=diceinput.GetLineFromCharIndex(curent_char);
            int select_start = diceinput.GetFirstCharIndexFromLine(curent_line);
            int select_end=diceinput.GetFirstCharIndexFromLine(curent_line+1)-1;
            //diceoutput.Text = select_end.ToString();
            if(select_end==-2)
            {
                select_end=diceinput.Text.Length;
            }
            int select_length=select_end-select_start;
            diceinput.Select(select_start, select_length);
            string selected = diceinput.SelectedText;
            diceinput.SelectedText = line_result(selected)+"\r\n";
        }

        //处理单行
        protected string line_result(string input)
        {
            input = input.Trim();
            //diceoutput.Text = input;
            if (Regex.IsMatch(input, @"^\\#"))
            {
                //diceoutput.Text = "it's a commet";
                return input;
            }
            if (!Regex.IsMatch(input, @"="))
            {
                //diceoutput.Text = "miss =";
                return input;
            }
            input = Regex.Replace(input,"=.*","=");
            string pat = @"(\d*)dp(?:\+(\d*))?(?:a(\d*))?=";
            Regex r = new Regex(pat, RegexOptions.IgnoreCase);
            Match m = r.Match(input);
            if (m.Success) 
            {
                int dp = Convert.ToInt32(m.Groups[1].Value);
                int autosuccess;
                if(m.Groups[2].Value=="") autosuccess=0;
                else autosuccess=Convert.ToInt32(m.Groups[2].Value);
                int again;
                if (m.Groups[3].Value == "") again = 0;
                else again=Convert.ToInt32(m.Groups[3].Value);
                string[] dice_result = loop(dp, again, autosuccess);
                string output = input + dice_result[1];
                diceoutput.Text = dice_result[0];
                //把结果放在1号框里一份
                string item_text = dice_result[1];
                string item_value = dice_result[1];
                if (checkBox1.Checked)     //极其丑陋的解决办法 byBX
                {
                    item_text = dice_result[0];
                }

                ListItem item = new ListItem(item_value, item_text);
                this.listBox1.Items.Add(item);
                this.listBox1.ValueMember = "ID";
                this.listBox1.DisplayMember = "Name";

                listBox1.SelectedItems.Clear();
                listBox1.SelectedItem = listBox1.Items[listBox1.Items.Count - 1];

                newest_vs();


                return output;
            }
            else
            {
                diceoutput.Text = "can't match";
                
            }
            //new DataTable().Compute("8+2*(3+4)", null);
            return input;
        }
  
    }
}